package com.example.tse.rbyair.widget.others;

import android.app.Dialog;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tse.rbhttpservice.service.goods.model.GoodsGetPriceResponse;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetResponse;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetSpecs;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetSpecsSpecValues;
import com.example.tse.rbyair.R;
import com.example.tse.rbyair.util.AppInforUtil;
import com.example.tse.rbyair.util.RbImageLoader;

import org.kymjs.kjframe.utils.DensityUtils;

/**
 * Created by hp on 16/1/26.
 */
public class ProductSpecificationPopwindow extends Dialog {

    private ActionSheetItemClickedListener actionSheetItemClickedListener;
    private ListView listview;
    private RelativeLayout productpop_relay;
    private RelativeLayout productpop_contentlay;
    private LinearLayout productpop_countlay;
    private Context context;
    private TextView cancle;
    private ScrollView productpop_scrollview;
    private ImageView productpop_dismiss;
    private TextView productpop_submit;
    private PlusLayout productpop_pluslay;
    private ListView productpop_speclist;
    private TextView productpop_price;
    private TextView productpop_mktprice;
    private TextView productpop_remaincount;
    private ImageView productpop_mainiv;
    private GoodsGetResponse t;
    private String isFastBuy = "0";

    public ProductSpecificationPopwindow(Context context, GoodsGetResponse t, String isFastBuy){
        super(context, R.style.myDialogTheme);
        this.context = context;
        this.t = t;
        this.isFastBuy = isFastBuy;
        init();
    }

    public ProductSpecificationPopwindow(Context context, int theme,GoodsGetResponse t) {
        super(context, R.style.myDialogTheme);
        this.context = context;
        this.t = t;
        init();
    }

    private void init(){
        View v = LayoutInflater.from(getContext()).inflate(R.layout.productdetial_select_window, null);
        Window window = this.getWindow();
        window.setWindowAnimations(R.style.AnimUp);
        this.setContentView(v);
        int w = DensityUtils.getScreenW(context);
        int h = DensityUtils.getScreenW(context);
        window.setLayout(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);

        productpop_relay = (RelativeLayout)v.findViewById(R.id.productpop_relay);
        productpop_scrollview = (ScrollView)v.findViewById(R.id.productpop_scrollview);
        productpop_dismiss = (ImageView)v.findViewById(R.id.productpop_dismiss);
        productpop_submit = (TextView)v.findViewById(R.id.productpop_submit);
        productpop_contentlay = (RelativeLayout)v.findViewById(R.id.productpop_contentlay);
        productpop_countlay = (LinearLayout)v.findViewById(R.id.productpop_countlay);
        productpop_pluslay = (PlusLayout)v.findViewById(R.id.productpop_pluslay);
        productpop_speclist = (ListView)v.findViewById(R.id.productpop_speclist);

        productpop_mainiv = (ImageView)v.findViewById(R.id.productpop_mainiv);
        TextView productpop_name = (TextView)v.findViewById(R.id.productpop_name1);
        productpop_price = (TextView)v.findViewById(R.id.productpop_price);
        productpop_mktprice = (TextView)v.findViewById(R.id.productpop_mktprice);
        productpop_remaincount = (TextView)v.findViewById(R.id.productpop_remaincount);

//		if(t.getPics().size()>0)
//		RbImageLoader.displayImage(t.getPics().get(0), productpop_mainiv, RbImageLoader.getLogoOptions());
        productpop_dismiss.setOnClickListener(new View.OnClickListener() {  //叉叉
            @Override
            public void onClick(View arg0) {
                dismiss();
            }
        });

        productpop_contentlay.setOnClickListener(new View.OnClickListener() {  //商品栏
            @Override
            public void onClick(View arg0) {
            }
        });

        productpop_countlay.setOnClickListener(new View.OnClickListener() {  //计数栏
            @Override
            public void onClick(View arg0) {
            }
        });

        productpop_relay.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent arg1) {
                dismiss();
                return false;
            }
        });

        this.setCancelable(true);
        this.setCanceledOnTouchOutside(true);


        if(t==null){
            return;
        }

        if(t.getName()!=null)
            productpop_name.setText(t.getName());

        String s = "";
        if(!TextUtils.isEmpty(t.getPrice())){
            s = "￥"+ AppInforUtil.formatPrice3(t.getPrice());
        }
        Spannable WordtoSpan = new SpannableString(s);
        WordtoSpan.setSpan(new AbsoluteSizeSpan(DensityUtils.dip2px(context, 11)), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        WordtoSpan.setSpan(new AbsoluteSizeSpan(DensityUtils.dip2px(context, 16)), 1, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        productpop_price.setText(WordtoSpan);

        String mktprice = "0";
        if(!TextUtils.isEmpty(t.getMktprice())){
            mktprice = AppInforUtil.formatPrice3(t.getMktprice());
        }
        SpannableString sp = new SpannableString("￥" + mktprice);
        sp.setSpan(new StrikethroughSpan(), 0, mktprice.length()+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        productpop_mktprice.setText(sp);

        productpop_remaincount.setText("限购" + t.getLimit() + "件");

        android.widget.RelativeLayout.LayoutParams lp = (android.widget.RelativeLayout.LayoutParams) productpop_scrollview.getLayoutParams();
        lp.width = w;
        lp.height = w/3;

        for(GoodsGetSpecs ggs:t.getSpecs()){
            for(GoodsGetSpecsSpecValues ggssv :ggs.getSpecValues()){
                if(t.getProductId().equals(""+ggssv.getProductId())){
                    RbImageLoader.displayImage(ggssv.getImageUrl(), productpop_mainiv, RbImageLoader.getLogoOptions());
                }
            }
        }

        ProductdetialTagAdapter detialTagAdapter = new ProductdetialTagAdapter(context, "0");

        productpop_speclist.setAdapter(detialTagAdapter);
        detialTagAdapter.setSpecs(t.getSpecs());
        detialTagAdapter.setCurrentProductId(t.getProductId());
        detialTagAdapter.setOnTagClickedListener(new ProductdetialTagAdapter.OnTagClickedListener() {
            @Override
            public void onTagClicked(final int position, final int position0) {
                if(actionSheetItemClickedListener!=null){

                    //价格改变
//                    GoodsGetPriceRequest r = new GoodsGetPriceRequest();
//                    r.setProductId(""+t.getSpecs().get(position).getSpecValues().get(position0).getProductId());
//                    r.setSecondId("");
//                    RemoteServiceFactory.getInstance().getGoodsService(context).getPrice(r, new RemoteServiceListener<GoodsGetPriceResponse>() {
//
//                        @Override
//                        public void ok(GoodsGetPriceResponse t) {
//
//                            StringBuffer sb = new StringBuffer();
//                            for(int i=0;i<t.getSpecs().size();i++){
//                                for(int j=0;j<t.getSpecs().get(i).getSpecValues().size();j++){
//                                    if(t.getProductId().equals(""+t.getSpecs().get(i).getSpecValues().get(j).getProductId())){
//                                        sb.append(t.getSpecs().get(i).getSpecValues().get(j).getSpecValue()+" ");
//                                    }
//                                }
//                            }
//
//                            RbLog.i("click=position="+position+"   "+t.getSpecs().get(position).getSpecValues().get(position0).getImageUrl());
//                            RbImageLoader.displayImage(t.getSpecs().get(position).getSpecValues().get(position0).getImageUrl(), productpop_mainiv, RbImageLoader.getProductDetialOptions());
//
//                            String s = "";
//                            if(!TextUtils.isEmpty(t.getPrice())){
//                                s = "￥"+CommonUtils.formatPrice3(t.getPrice());
//                            }
//                            Spannable WordtoSpan = new SpannableString(s);
//                            WordtoSpan.setSpan(new AbsoluteSizeSpan(CommonUtils.dip2px(context, 11)), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//                            WordtoSpan.setSpan(new AbsoluteSizeSpan(CommonUtils.dip2px(context, 16)), 1, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//                            productpop_price.setText(WordtoSpan);
//
//                            String mktprice = "0";
//                            if(!TextUtils.isEmpty(t.getMktprice())){
//                                mktprice = CommonUtils.formatPrice3(t.getMktprice());
//                            }
//                            SpannableString sp = new SpannableString("￥" + mktprice);
//                            sp.setSpan(new StrikethroughSpan(), 0, mktprice.length()+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//                            productpop_mktprice.setText(sp);
//
//                            productpop_pluslay.setMaxCount(Integer.parseInt(t.getLimit()));
//                            if(productpop_pluslay.getCount() >Integer.parseInt(t.getLimit())){
//                                productpop_pluslay.setCount(Integer.parseInt(t.getLimit()));
//                            }
//
//                            productpop_remaincount.setText("限购" + t.getLimit() + "件");
//
//                            actionSheetItemClickedListener.OnTagClicked(t,sb.toString());
//                            //
//                        }
//
//                        @Override
//                        public void failue(int errorCode, String errorMessage) {
//
//                        }
//                    });
                }
            }
        });


        productpop_submit.setOnClickListener(new View.OnClickListener() {   //提交
            @Override
            public void onClick(View arg0) {
                dismiss();
                if(actionSheetItemClickedListener!=null)
                    actionSheetItemClickedListener.OnSubmit();
            }
        });


        if(isFastBuy!=null && !isFastBuy.equals("2") && !isFastBuy.equals("4")){   //秒杀 或者 专享 固定数目为1不能点
            productpop_pluslay.setOnNumChangeListener(new PlusLayout.OnNumChangeListener() {

                @Override
                public void onChanged(int n) {
                    actionSheetItemClickedListener.OnCountChanged(n);
                }
            });
        }else{
            productpop_pluslay.setOnNumChangeListener(null);
        }

        if(!TextUtils.isEmpty(t.getLimit())){
            productpop_pluslay.setMaxCount(Integer.parseInt(t.getLimit()));
            productpop_pluslay.setOnMaxListener(new PlusLayout.OnMaxListener() {

                @Override
                public void onMaxHint() {
                    Toast.makeText(context, "已达限购上限", Toast.LENGTH_SHORT).show();
                }
            });
        }


    }

    public interface ActionSheetItemClickedListener{
        void OnSubmit();
        void OnCountChanged(int n);
        void OnTagClicked(GoodsGetPriceResponse t,String tagtxt);
    }

    public void setOnActionSheetItemClickedListener(ActionSheetItemClickedListener actionSheetItemClickedListener){
        this.actionSheetItemClickedListener = actionSheetItemClickedListener;
    }


}
